using System;
using System.Drawing;
using System.ComponentModel;

namespace WebChart
{
	public class PieChart : Chart
	{
		// Fields
		private Color[] colors;
		private int explosion;

		// Methods
		public PieChart()
		{
		}

		public PieChart(ChartPointCollection data) : base(data)
		{
		}

		public PieChart(ChartPointCollection data, Color color) : base(data, color)
		{
		}

		private void DrawDataLabels(Graphics graphics, float angleScale, float x, float y, int width)
		{
			float num = 0f;
			float num2 = 270f;
			float num3 = x;
			float num4 = y;
			float num5 = ((float) width) / 2f;
			float num6 = num5;
			StringFormat format = null;
			format = new StringFormat();
			format.Alignment = StringAlignment.Center;
			format.LineAlignment = StringAlignment.Near;
			Pen pen = base.dataLabels.border.GetPen(graphics);
			Brush backgroundBrush = base.dataLabels.background.GetBrush(graphics);
			Brush textBrush = new SolidBrush(base.dataLabels.foreColor);
			if (base.dataLabels.Position == DataLabelPosition.Center)
			{
				num6 *= 0.6f;
			}
			int dataLabelMaxWidth = this.DataLabelMaxWidth;
			num = 0f;
			num2 = 270f;
			foreach (ChartPoint point in base.Data)
			{
				PointF location;
				num = angleScale * point.yvalue;
				float num8 = num / 2f;
				if (this.explosion != 0)
				{
					float num9 = ((num2 + num8) * 3.141593f) / 180f;
					float num10 = ((float) Math.Cos((double) num9)) * this.explosion;
					float num11 = ((float) Math.Sin((double) num9)) * this.explosion;
					x = num3 + num10;
					y = num4 + num11;
				}
				string dataLabelText = this.GetDataLabelText(point);
				SizeF size = graphics.MeasureString(dataLabelText, base.dataLabels.font, dataLabelMaxWidth, format);
				float num12 = num2 * 0.01745329f;
				float num13 = num8 * 0.01745329f;
				float num14 = (x + num5) + (num6 * ((float) Math.Cos((double) (num12 + num13))));
				float num15 = (y + num5) + (num6 * ((float) Math.Sin((double) (num12 + num13))));
				if (base.dataLabels.Position == DataLabelPosition.Center)
				{
					location = new PointF(num14 - (size.Width / 2f), num15 - (size.Height / 2f));
				}
				else
				{
					float num16 = num2 + num8;
					if (num16 < 360f)
					{
						location = new PointF(num14 + 1f, num15 - size.Height);
					}
					else if (num16 < 450f)
					{
						location = new PointF(num14 + 1f, num15);
					}
					else if (num16 < 540f)
					{
						location = new PointF(num14 - size.Width, num15);
					}
					else
					{
						location = new PointF(num14 - size.Width, num15 - size.Height);
					}
				}
				RectangleF rectangle = new RectangleF(location, size);
				if ((point.yvalue != 0f) || base.dataLabels.showZeroValues)
				{
					this.DrawDataLabel(graphics, dataLabelText, rectangle, pen, backgroundBrush, format, textBrush);
				}
				num2 += num;
			}
		}

		private void DrawPie(Graphics graphics, float angleScale, float x, float y, int width, int height)
		{
			bool flag = (base.shadow != null) && base.Shadow.Visible;
			Brush brush = null;
			if (flag)
			{
				brush = new SolidBrush(base.Shadow.Color);
			}
			Pen pen = base.Line.GetPen(graphics);
			float num = x;
			float num2 = y;
			float sweepAngle = 0f;
			float startAngle = 270f;
			int num5 = 0;
			int num6 = this.colors.Length - 1;
			foreach (ChartPoint point in base.Data)
			{
				if (num5 > num6)
				{
					num5 = 0;
				}
				Color color = this.colors[num5++];
				sweepAngle = angleScale * point.yvalue;
				if (this.explosion != 0)
				{
					float num7 = ((startAngle + (sweepAngle / 2f)) * 3.141593f) / 180f;
					float num8 = ((float) Math.Cos((double) num7)) * this.explosion;
					float num9 = ((float) Math.Sin((double) num7)) * this.explosion;
					x = num + num8;
					y = num2 + num9;
				}
				if (flag)
				{
					graphics.FillPie(brush, x + base.Shadow.OffsetX, y + base.Shadow.OffsetY, (float) width, (float) height, startAngle, sweepAngle);
				}
				graphics.FillPie(new SolidBrush(color), x, y, (float) width, (float) height, startAngle, sweepAngle);
				graphics.DrawPie(pen, x, y, (float) width, (float) height, startAngle, sweepAngle);
				startAngle += sweepAngle;
			}
		}

		public override void Render(Graphics graphics, int width, int height)
		{
			ChartEngine engine = base.Engine;
			engine.GridLines = GridLines.None;
			engine.ShowXValues = false;
			engine.ShowYValues = false;
			float x = 0f;
			float y = -height;
			y += this.explosion;
			x += this.explosion;
			height -= 10 + (2 * this.explosion);
			width -= 10 + (2 * this.explosion);
			if (height >= width)
			{
				y += (height - width) / 2;
				height = width;
			}
			else
			{
				x += (width - height) / 2;
				width = height;
			}
			if (width > 0)
			{
				float num3 = 0f;
				foreach (ChartPoint point in base.Data)
				{
					num3 += point.yvalue;
				}
				float angleScale = 360f / num3;
				if ((this.colors == null) || (this.colors.Length == 0))
				{
					this.colors = new Color[] { Color.SteelBlue, Color.DarkSlateBlue, Color.LightSteelBlue, Color.LemonChiffon, Color.MediumSlateBlue, Color.Lavender, Color.DarkKhaki, Color.Blue, Color.Cyan, Color.AntiqueWhite };
				}
				int dataLabelMaxWidth = this.DataLabelMaxWidth;
				if (base.dataLabels.Visible)
				{
					if (base.dataLabels.font == null)
					{
						base.dataLabels.font = ChartText.DefaultFont;
					}
					float num6 = 0f;
					foreach (ChartPoint point2 in base.Data)
					{
						string dataLabelText = this.GetDataLabelText(point2);
						SizeF ef = graphics.MeasureString(dataLabelText, base.dataLabels.font, dataLabelMaxWidth);
						if (ef.Height > num6)
						{
							num6 = ef.Height;
						}
						if (ef.Width > num6)
						{
							num6 = ef.Width;
						}
					}
					x += num6;
					y += num6;
					height -= (int) (num6 * 2f);
					width = height;
				}
				if (width > 0)
				{
					this.DrawPie(graphics, angleScale, x, y, width, height);
					if (base.dataLabels.visible)
					{
						this.DrawDataLabels(graphics, angleScale, x, y, width);
					}
				}
			}
		}

		// Properties
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
		public Color[] Colors
		{
			get
			{
				return this.colors;
			}
			set
			{
				this.colors = value;
			}
		}

		[DefaultValue(0), Browsable(true)]
		public int Explosion
		{
			get
			{
				return this.explosion;
			}
			set
			{
				this.explosion = value;
			}
		}
	}
}
